package com.sergiandreplace.kaboom.format;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;


public abstract class FormatManager
{
	private String uri;
	protected ArrayList<String> filenames;
	private FormatBitmapExtractorThread formatThread;
	private OnBitmapLoadedListener onFileLoadedListener = null;
	private MessageHandler handler = new MessageHandler();

	public FormatManager(String uri)
	{
		setUri(uri);
	}

	public int getFilenames()
	{
		if (filenames != null)
			return filenames.size();
		else
			return 0;
	}

	public void setUri(String uri)
	{
		this.uri = uri;
	}

	public String getUri()
	{
		return uri;
	}

	public abstract boolean loadFileInfo();

	public abstract void loadFilenames(String pattern);

	public void loadBitmap(int index, int size)
	{
		String filename = filenames.get(index);
		loadBitmap(filename, size);
	}

	public void setFormatExtractorThread(FormatBitmapExtractorThread formatThread)
	{
		this.formatThread = formatThread;
	}

	public void loadBitmap(String filename, int size)
	{
		if (formatThread != null) formatThread.start();
	}
	
	public class MessageHandler extends Handler
	{

		public void handleMessage(Message msg)
		{

			super.handleMessage(msg);
			if (onFileLoadedListener != null) onFileLoadedListener.onBitmapLoaded((Bitmap)msg.getData().getParcelable("Bitmap"));

		}
	}
	public interface OnBitmapLoadedListener
	{
		public void onBitmapLoaded(Bitmap result);
	}

	public OnBitmapLoadedListener getOnBitmapLoadedListener()
	{
		return onFileLoadedListener;
	}

	public void setOnBitmapLoadedListener(OnBitmapLoadedListener onBitmapLoadedListener)
	{
		this.onFileLoadedListener = onBitmapLoadedListener;
	}

	public void setHandler(MessageHandler handler)
	{
		this.handler = handler;
	}

	public MessageHandler getHandler()
	{
		return handler;
	}

}
